package logic.slots;

import logic.nodes.SlotNode;

import com.jme.bounding.BoundingBox;
import com.jme.bounding.BoundingSphere;
import com.jme.bounding.BoundingVolume;
import com.jme.math.Vector3f;
import com.jme.scene.Node;
import com.jme.scene.TriMesh;

public abstract class NodeSlot extends Node implements Slot {
	
	private static final long serialVersionUID = 1L;
	
	protected SlotNode slotNode;
	protected TriMesh[] meshs;
	protected SlotController slotController;
	
	public NodeSlot(String name, SlotNode slotNode, TriMesh... meshs) {
		super(name);
		this.slotNode = slotNode;
		this.meshs = meshs;
		
		slotController = new SlotController(this);
		addController(slotController);
		
		slotNode.registerSlot(this);
	}
	
	@Override
	public void removeSlotController() { removeController(slotController); }
	
	protected float getMaxSizeOf(BoundingVolume volume) {
		if(volume instanceof BoundingSphere) return ((BoundingSphere)volume).radius;
		assert(volume instanceof BoundingBox);
		BoundingBox box = (BoundingBox)volume;
		if(box.xExtent > box.yExtent && box.xExtent > box.zExtent) return box.xExtent;
		else if(box.yExtent > box.xExtent && box.yExtent > box.zExtent) return box.yExtent;
		return box.zExtent;
	}
	
	protected Vector3f getLocalPos(TriMesh part) {
		assert(slotNode != null);
		assert(part != null);
		assert(part.getWorldBound() != null);
		return slotNode.worldToLocal(part.getWorldBound().getCenter(), new Vector3f());
	}
	
	@Override
	public void unregisterSlot() {
		slotNode.slotDone(this);
	}
	
	@Override
	public TriMesh[] getSlotMeshs() { return meshs; }
}